﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.Entities;
using System.Configuration;
using System.Web.Security;

namespace LibrarySystem.LibraryUtils.DAL.DAOs
{
    public class Library_Employees : Common
    {

        #region Employees
        public List<EmployeeModel> getEmpToComboBox(string userName = null)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();

            IEnumerable<Library_Employee> queryEmp = _context.Library_Employees;
            //Filter
            if (!string.IsNullOrEmpty(userName))
            {
                queryEmp = queryEmp.Where(e => e.aspnet_User.UserName.Contains(userName));
            }
            List<Library_Employee> employees = queryEmp.ToList();

            List<EmployeeModel> empList = new List<EmployeeModel>();

            foreach (Library_Employee e in employees)
            {
                EmployeeModel data = new EmployeeModel();
                data.ID = e.Id;
                data.UserName = e.aspnet_User.UserName;
                empList.Add(data);
            }
            return empList;
        }

        public int GetDataCount(string name = null)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_Employee> queryEmp = _context.Library_Employees;
            //Filter
            if (!string.IsNullOrEmpty(name))
            {
                queryEmp = queryEmp.Where(e => (e.FirstName + " " + e.LastName).Contains(name));
            }
            return queryEmp.Count();
        }

        public List<EmployeeModel> GetDataList(int startIndex = 0, int pageSize = 0, string sorting = null,
            string name = null)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_Employee> queryEmp = _context.Library_Employees;
            //Filter
            if (!string.IsNullOrEmpty(name))
            {
                queryEmp = queryEmp.Where(e => (e.FirstName + " " + e.LastName).Contains(name));
            }
            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("FirstName ASC"))
            {
                queryEmp = queryEmp.OrderBy(e => e.FirstName);
            }
            else if (sorting.Equals("FirstName DESC"))
            {
                queryEmp = queryEmp.OrderByDescending(e => e.FirstName);
            }
            List<Library_Employee> employees = pageSize > 0 ?
                                                queryEmp.Skip(startIndex).Take(pageSize).ToList()
                                                : queryEmp.ToList();
            List<EmployeeModel> empList = new List<EmployeeModel>();
            foreach (Library_Employee e in employees)
            {
                EmployeeModel data = new EmployeeModel();
                data.ID = e.Id;
                data.UserName = e.aspnet_User.UserName;
                data.FirstName = e.FirstName;
                data.LastName = e.LastName;
                data.BirthDay = (DateTime)e.BirthDay;
                data.Email = e.Email;
                empList.Add(data);
            }
            _context.Dispose();
            return empList;
        }

        public EmployeeModel AddNewData(EmployeeModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Employee emp = new Library_Employee();
            //Assign parameter
            emp.UserId = base.CreateUserAccount(data.UserName, ConfigurationManager.AppSettings["DefaultPassword"],
                        ConfigurationManager.AppSettings["AdminRole"]);
            emp.FirstName = data.FirstName;
            emp.LastName = data.LastName;
            emp.BirthDay = (DateTime)data.BirthDay;
            emp.Email = data.Email;
            //Insert into database
            _context.Library_Employees.InsertOnSubmit(emp);
            _context.SubmitChanges();
            //Assign ID after inserted
            data.ID = emp.Id;
            //
            _context.Dispose();
            return data;
        }

        public bool UpdateData(EmployeeModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Employee emp = _context.Library_Employees.Where(e => e.Id == data.ID).FirstOrDefault();
            //
            emp.FirstName = data.FirstName;
            emp.LastName = data.LastName;
            emp.BirthDay = (DateTime)data.BirthDay;
            emp.Email = data.Email;
            //Update into database
            _context.SubmitChanges();
            //
            _context.Dispose();
            return true;
        }

        public bool DeleteData(int Id)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code

            //
            _context.Dispose();
            return true;
        }

        #endregion
    }
}